package com.sdk.es.http.controller;

import com.sdk.es.http.service.base.IEsBaseService;
import com.sdk.es.sdk.entity.common.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wst
 * @date 2024/1/31
 *
 * 父类 Controller
 * @param <E>   具体索引实体
 */
@Slf4j
public abstract class EsBaseController<E, S extends IEsBaseService<E>> {

    @Resource
    protected S baseService;

    @PostMapping("/save")
    public EsResult<E> save(@RequestBody E req) {
        return EsResult.success(baseService.save(req));
    }

    @PostMapping("/saveAsync")
    public EsResult saveAsync(@RequestBody E req) {
        baseService.saveAsync(req);
        return EsResult.success();
    }

    @PostMapping("/saveBatch")
    public EsResult<List<E>> saveBatch(@RequestBody List<E> list) {
        return EsResult.success(baseService.saveBatch(list));
    }

    @PostMapping("/saveBatchAsync")
    public EsResult saveBatchAsync(@RequestBody List<E> list) {
        baseService.saveBatchAsync(list);
        return EsResult.success();
    }

    @PostMapping("/update")
    public EsResult update(@RequestBody E req) {
        baseService.update(req);
        return EsResult.success();
    }

    @PostMapping("/updateAsync")
    public EsResult updateAsync(@RequestBody E req) {
        baseService.updateAsync(req);
        return EsResult.success();
    }

    @PostMapping("/delete")
    public EsResult delete(@RequestBody E req) {
        baseService.delete(req);
        return EsResult.success();
    }

    @PostMapping("/deleteAsync")
    public EsResult deleteAsync(@RequestBody E req) {
        baseService.deleteAsync(req);
        return EsResult.success();
    }

    @PostMapping("/getById")
    public EsResult<E> getById(@RequestBody E req) {
        return EsResult.success(baseService.getById(req));
    }

    @PostMapping("/getOne")
    public EsResult<E> getOne(@RequestBody EsBaseParams<E> req) {
        return EsResult.success(baseService.getOne(req));
    }

    @PostMapping("/getList")
    public EsResult<List<E>> getList(@RequestBody EsBaseParams<E> req) {
        return EsResult.success(baseService.getList(req));
    }

    @PostMapping("/page")
    public EsResult<EsPageResult<E>> page(@RequestBody EsPageReq<E> pageReq) {
        return EsResult.success(baseService.page(pageReq));
    }

    @PostMapping("/count")
    public EsResult<Long> count(@RequestBody EsBaseParams<E> req) {
        return EsResult.success(baseService.count(req));
    }

    @PostMapping("/groupBy")
    public EsResult<List<GroupByResp>> groupBy(@RequestBody EsBaseParams<E> req) {
        return EsResult.success(baseService.groupBy(req));
    }

    @PostMapping("/groupByPage")
    public EsResult<EsPageResult<GroupByResp>> groupByPage(@RequestBody EsPageReq<E> pageReq) {
        return EsResult.success(baseService.groupByPage(pageReq));
    }

    @PostMapping("/sum")
    public EsResult<Double> sum(@RequestBody EsBaseParams<E> req) {
        return EsResult.success(baseService.sum(req));
    }
}
